﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers.Email
{
    public class EmailController : BaseController
    {
        string sControllerName = "Email";
        // GET: Email
        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            SGArchiveSystem7x.App_Code.CSession.Clear_PageSession(Session);
            return View();
        }

        [HttpPost]
        public ActionResult Get_ajaxEmailList()
        {
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_EmailDal.Get_Email(-1,out count, 0, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Get_ajaxEmailListPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_EmailDal.Get_Email(-1,out count, iStart, SGArchiveSystem7x.App_Code.CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据ID获取某一个邮箱
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxEmail(int id)
        {
            var en = SGArchiveSystem7xDB.Linq.T_EmailDal.Get_Email(id);
            var json = new
            {
                en
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public string Set_ajaxAddEmail(int id, int type, string name, string server, string pwd)
        {
            var en = new SGArchiveSystem7xDB.T_Email()
            {
                E_EMAIL = name,
                E_PWD = pwd,
                E_TYPE = type,
                E_USER = server
            };
            if (id == 0)
            {
                var value = (int)SGArchiveSystem7xDB.Linq.T_EmailDal.Add_Email(en);
                return value.ToString();
            }
            else
            {
                en.E_ID = id;
                var value = (int)SGArchiveSystem7xDB.Linq.T_EmailDal.Modify_Email(en);
                return value.ToString();
            }
        }

        /// <summary>
        /// 删除邮箱
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string Set_ajaxDelEmail(int id)
        {
            var value = (int)SGArchiveSystem7xDB.Linq.T_EmailDal.Del_Email(id);
            return value.ToString();
        }
        
    }
}